package com.service.sys;

import com.common.ErrorCodeEnum;
import com.common.emums.DataStatus;
import com.common.exception.BusinessException;
import com.common.utils.StringUtils;
import com.core.springSecurity.common.SecurityUtils;
import com.dao.sys.SysUserRoleMapper;
import com.model.pojo.sys.SysRole;
import com.model.pojo.sys.SysUserRole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Author: zql
 * @Description: 用户角色服务
 * @Data: 15:17 2021/7/5
 */
@Slf4j
@Service
public class SysUserRoleService {
    @Resource
    SysUserRoleMapper sysUserRoleMapper;

    @Resource
    SysUserService sysUserService;

    @Resource
    SysRoleService sysRoleService;

    /**
     * @Author: zql
     * @Description: 查询用户绑定的角色列表
     * @param sysUserId 用户id
     * @Data: 10:24 2021/7/6
     */
    public List<SysRole> getRoleList(Long sysUserId) {
        List<SysUserRole> sysUserRoleList = sysUserRoleMapper.selectListByUserId(sysUserId);
        List<SysRole> roleList = new ArrayList<>();
        for (SysUserRole userRole : sysUserRoleList) {
            try {
                SysRole sysRole = sysRoleService.get(userRole.getSysRoleId(),null);
                if(sysRole.getIsDelete().equals(DataStatus.OK.getCode())) {
                    roleList.add(sysRole);
                } else{
                    log.error("无法根据用户查询角色，用户绑定的角色状态:{} userId:{},roleId:{}",DataStatus.info(sysRole.getIsDelete()),sysUserId, sysRole.getSysRoleId());
                }
            } catch (BusinessException e) {
                log.error("无法根据用户查询角色，用户绑定了不存在的角色userId:{},roleId:{},code:{},msg:{}", sysUserId, userRole.getSysRoleId(),
                        e.getErrorCodeEnum().getErrorCode(), e.getErrorCodeEnum().getErrorMsg());
            }
        }
        return roleList;
    }

    public int getUserCount(Long sysRoleId) {
        return sysUserRoleMapper.countByRoleId(sysRoleId);
    }

    /**
     * @Author: zql
     * @Description: 校验队列中用户是否存在
     * @param userIds 用户id列表
     * @Data: 10:06 2021/5/12
     */
    public void verifySysUserId(Set<Long> userIds){
        for (Long sysUserId:userIds) {
            if (StringUtils.isNull(sysUserId)) {
                throw new BusinessException(ErrorCodeEnum.PARAM_FAILED);
            }
            if (!sysUserService.has(sysUserId)) {
                if(sysUserService.has(sysUserId,DataStatus.DISABLE.getCode())){
                    throw new BusinessException(ErrorCodeEnum.USER_STOP.setParam(sysUserId.toString()));
                }
                if(sysUserService.has(sysUserId,DataStatus.DELETED.getCode())){
                    throw new BusinessException(ErrorCodeEnum.USER_DELETE.setParam(sysUserId.toString()));
                }
                throw new BusinessException(ErrorCodeEnum.USER_DATA_NOT_FOUND.setParam("id:"+sysUserId.toString()));
            }
        }
    }

    public void uBindList(Long roleId){
        sysUserRoleMapper.deleteBySysRoleId(roleId);
    }

    /**
     * 批量绑定角色与账号
     *
     * @param roleId 角色Id
     * @param sysUserIdList 用户Id列表
     */
    public void bindingRole(Long roleId, Set<Long> sysUserIdList) {
        if (sysUserIdList.size() == 0) {
            return;
        }
        String name = SecurityUtils.getUsername();
        List<SysUserRole> sysUserRoleList = new ArrayList<>();
        for (Long sysUserId : sysUserIdList) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setSysRoleId(roleId);
            sysUserRole.setSysUserId(sysUserId);
            sysUserRole.setModifier(name);
            sysUserRole.setCreator(name);
            sysUserRoleList.add(sysUserRole);
        }
        sysUserRoleMapper.insertSysUserRoleList(sysUserRoleList);
    }

    public void bindingUser(Long userId, Set<Long> sysRoleIdSet) {
        if (sysRoleIdSet.size() == 0) {
            return;
        }
        String name = SecurityUtils.getUsername();
        List<SysUserRole> sysUserRoleList = new ArrayList<>();
        for (Long sysRoleId : sysRoleIdSet) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setSysRoleId(sysRoleId);
            sysUserRole.setSysUserId(userId);
            sysUserRole.setModifier(name);
            sysUserRole.setCreator(name);
            sysUserRoleList.add(sysUserRole);
        }
        sysUserRoleMapper.insertSysUserRoleList(sysUserRoleList);
    }
}
